package com.daJiao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daJiao.constants.Constants;
import com.daJiao.domain.*;
import com.daJiao.entity.SysRole;
import com.daJiao.enums.DeleteFlagEnums;
import com.daJiao.holder.SecurityUserHolder;
import com.daJiao.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>Title:	  UserController. </p>
 * <p>Description 用户controller </p>
 * <p>Company:   医百科技 </p>
 *
 * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
 * @CreateDate 2021 /12/10 19:05
 */
@RestController
@RequestMapping(value = "/sysRole")
@Api(tags = "角色管理")
public class SysRoleController {

    @Resource
    private SysRoleService roleService;

    @Resource
    private SecurityUserHolder securityUserHolder;

    /**
     * <p>Title:	  listPage. </p>
     * <p>Description 分页查询角色列表</p>
     *
     * @param pageParam the role
     * @return the wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @CreateDate 2017 /8/17 19:04
     */
    @PostMapping(value = "/page")
    @ApiOperation(httpMethod = "POST", value = "分页查询角色列表")
    public Wrapper<Page<SysRole>> page(@ApiParam(name = "role", value = "角色信息") @RequestBody PageParam<SysRole> pageParam) {
        Page<SysRole> listPage = roleService.listPage(pageParam);
        return WrapMapper.wrap(listPage);
    }

    /**
     * <p>Title:      delete. </p>
     * <p>Description 根据id删除角色</p>
     *
     * @param id the id
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2018 /2/20 14:14
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除角色")
    public Wrapper<Integer> delete(@PathVariable String id) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        return WrapMapper.ok(roleService.delete(id, securityUser));
    }

    /**
     * <p>Title:      update. </p>
     * <p>Description 修改角色信息</p>
     *
     * @param role the role
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/20 15:38
     */
    @PutMapping
    @ApiOperation(httpMethod = "POST", value = "修改角色信息")
    public Wrapper<Integer> update(@ApiParam(name = "role", value = "角色信息") @RequestBody SysRole role) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        role.setUpdateBy(securityUser.getUserId());
        role.setUpdater(securityUser.getRealName());
        role.setUpdateTime(LocalDateTime.now());
        return WrapMapper.ok(roleService.updateRole(role));
    }

    /**
     * <p>Title:      save. </p>
     * <p>Description 保存角色信息</p>
     *
     * @param role the role
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2018 /2/22 17:24
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "保存角色信息")
    public Wrapper<Integer> save(@ApiParam(name = "role", value = "角色信息") @RequestBody SysRole role) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        role.setCreateBy(securityUser.getUserId());
        role.setCreator(securityUser.getRealName());
        role.setUpdateBy(securityUser.getUserId());
        role.setUpdater(securityUser.getRealName());
        int result = roleService.saveRole(role);
        return WrapMapper.ok(result);
    }

    /**
     * <p>Title:      getRoleList. </p>
     * <p>Description 查詢所有角色信息</p>
     *
     * @return List<SysRole>
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2019/4/11 14:35
     */
    @GetMapping(value = "/list")
    @ApiOperation(httpMethod = "GET", value = "查询角色信息")
    public Wrapper<List<SysRole>> list() {
        List<SysRole> roleList = roleService.list(new QueryWrapper<SysRole>().lambda().eq(SysRole::getDelFlag, DeleteFlagEnums.NORMAL.getStatus()).eq(SysRole::getStatus, Constants.ENABLE));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, roleList);
    }

    /**
     * <p>Title:      roleBindResource. </p>
     * <p>Description 角色绑定资源(包括菜单和按钮权限)</p>
     *
     * @param roleBindResource RoleBindResource
     * @return void
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2019/4/10 20:18
     */
    @PostMapping(value = "/bind/resource")
    @ApiOperation(httpMethod = "POST", value = "角色绑定资源")
    public Wrapper<?> roleBindResource(@RequestBody RoleBindResource roleBindResource) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        roleService.roleBindResource(roleBindResource, securityUser);
        return WrapMapper.ok();
    }

    /**
     * <p>Title:      getBindResourceByRoleId. </p>
     * <p>Description 获取角色绑定的资源信息（包括菜单和按钮）</p>
     *
     * @param roleId String
     * @return List<String>
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2019/4/10 21:21
     */
    @GetMapping(value = "/bind/resource/{roleId}")
    @ApiOperation(httpMethod = "GET", value = "获取角色绑定的资源")
    public Wrapper<List<String>> getBindResourceByRoleId(@PathVariable String roleId) {
        List<String> resourceIds = roleService.getBindResourceByRoleId(roleId);
        return WrapMapper.ok(resourceIds);
    }

    /**
     * <p>Title:      roleBindUser. </p>
     * <p>Description 角色绑定用户</p>
     *
     * @param roleBindUser RoleBindUser
     * @return Boolean
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2019/4/10 20:18
     */
    @PostMapping(value = "/bind/user")
    @ApiOperation(httpMethod = "POST", value = "角色绑定用户")
    public Wrapper<Boolean> roleBindUser(@RequestBody RoleBindUser roleBindUser) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        roleService.roleBindUser(roleBindUser, securityUser);
        return WrapMapper.ok();
    }

    /**
     * 获取角色绑定的用户信息
     *
     * @param roleId String
     * @return List<String>
     */
    @GetMapping(value = "/bind/user/{roleId}")
    @ApiOperation(httpMethod = "GET", value = "获取角色绑定的用户信息")
    public Wrapper<List<String>> getBindUserByRoleId(@PathVariable String roleId) {
        List<String> userIds = roleService.getBindUserByRoleId(roleId);
        return WrapMapper.ok(userIds);
    }


}
